package com.shou.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.shou.CommonVar;
import com.shou.dao.ServeInfoDao;
import com.shou.dao.support.EnhancedRule;
import com.shou.dao.support.Page;
import com.shou.entity.ServeInfo;

@Repository("serveInfoDaoImpl")
public class ServeInfoDaoImpl extends BaseDaoImpl<ServeInfo> implements ServeInfoDao {

	@Override
	public List<ServeInfo> queryList(ServeInfo serveInfoQuery) {
		// TODO Auto-generated method stub
		EnhancedRule rule = new EnhancedRule();
		if(serveInfoQuery.getUseStatus() == null) {
			rule.add(Restrictions.eq("useStatus",CommonVar.USE_ONUSE));
		}
		else {
			rule.add(Restrictions.eq("useStatus",serveInfoQuery.getUseStatus()));
		}
		if(serveInfoQuery.getUserId()!=null) {
			rule.add(Restrictions.eq("userId",serveInfoQuery.getUserId()));
		}
		return this.query(ServeInfo.class, rule);
	}

	@Override
	public Page<ServeInfo> queryPage(int pageNum, int pageSize, ServeInfo query) {
		// TODO Auto-generated method stub
		EnhancedRule rule = new EnhancedRule();
		
		rule.setOffset(pageNum>0?(pageNum - 1)*pageSize:0);
		rule.setPageSize(pageSize);		
		rule.addOrder(Order.asc("serveStatus"));
		rule.addOrder(Order.desc("id"));
		return this.queryForPage(ServeInfo.class, rule);
	}

	@Override
	public List<ServeInfo> getUserCollectServe(Long userId) {
		// TODO Auto-generated method stub
		String hql = "select new ServeInfo(s.id,s.serveTypeId,s.title,s.districtName,s.discription,s.address,s.contactPhone,s.contactName,s.serveStatus,s.createTime) from Collection c,ServeInfo s "
				+ "where c.serveId = s.id and c.userId=:userId and s.useStatus = 1 and c.useStatus = 1 order by s.id desc";
		Map<String, Object> params = new HashMap<>();
		params.put("userId",userId);
		return this.find(hql, params);
	}

}
